local skel = fk.CreateSkill {
  name = "rmt__pianshi",
  dynamic_desc = function (self, player, lang)
    local arg1, arg2 = Fk:translate("rmt__pianshi_change", lang), Fk:translate("rmt__pianshi_coincide", lang)
    if player:getMark("rmt__pianshi_change-turn") ~= 0 then arg1 = "<font color='grey'>"..arg1.."</font>" end
    if player:getMark("rmt__pianshi_coincide-turn") ~= 0 then arg2 = "<font color='grey'>"..arg2.."</font>" end
    return "rmt__pianshi_dyn:" .. arg1 .. ":" .. arg2
  end,
}

Fk:loadTranslationTable{
  ["rmt__pianshi"] = "偏师",
  [":rmt__pianshi"] = "每回合各限一次：你完成一个连招时，可改为执行另一个连招技的效果；你同时完成多个连招时，摸三张牌。",
  [":rmt__pianshi_dyn"] = "每回合各限一次：{1}；{2}。",

  ["rmt__pianshi_change"] = "你完成一个连招时，可改为执行另一个连招技的效果",
  ["rmt__pianshi_coincide"] = "你同时完成多个连招时，摸三张牌",
  ["#rmt__pianshi-invoke"] = "偏师：是否要将“%arg”的连招改为另一个连招技的效果？",
  ["#RmtPianshiLog"] = "%from 的连招【%arg】改为【%arg2】",

  ["$rmt__pianshi1"] = "正兵击首，奇兵衔尾，孰堪当之！",
  ["$rmt__pianshi2"] = "用兵速则难变，独吾兼得！",
}


skel:addEffect(fk.AfterCardUseDeclared, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and
      data.extra_data and data.extra_data.combo_skill then
      local cost_data = {choices = {}}
      local n = 0
      for _, _ in pairs(data.extra_data.combo_skill) do
        n = n + 1
      end
      if n > 1 and player:getMark("rmt__pianshi_coincide-turn") == 0 then
        table.insert(cost_data.choices, "coincide")
      end
      if player:getMark("rmt__pianshi_change-turn") == 0 then
        local skills = {}
        for _, skill in ipairs(player.player_skills) do
          if skill:isPlayerSkill(player) and skill.visible and skill:hasTag(Skill.Combo) then
            table.insert(skills, skill.name)
          end
        end
        if #skills > 1 then
          table.insert(cost_data.choices, "change")
          cost_data.skill_choices = skills
        end
      end
      if #cost_data.choices > 0 then
        event:setCostData(self, cost_data)
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local cost_data = event:getCostData(self)
    if table.contains(cost_data.choices, "change") then
      local all_choices = table.simpleClone(cost_data.skill_choices)
      local skills = {}
      for skill, _ in pairs(data.extra_data.combo_skill) do
        table.insert(skills, skill)
      end
      table.insert(all_choices, "Cancel")
      for _, skill in ipairs(skills) do
        local choices = table.simpleClone(all_choices)
        table.removeOne(choices, skill)
        local choice = player.room:askToChoice(player, {
          choices = choices, all_choices = all_choices, skill_name = skel.name, prompt = "#rmt__pianshi-invoke:::"..skill,
        })
        if choice ~= "Cancel" then
          cost_data.change = {skill, choice}
          break
        end
      end
      if not cost_data.change then
        table.removeOne(cost_data.choices, "change")
      end
    end
    if #cost_data.choices == 0 then return false end
    event:setCostData(self, cost_data)
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    if cost_data.change then
      room:setPlayerMark(player, "rmt__pianshi_change-turn", 1)
      local from, to = table.unpack(cost_data.change)
      data.extra_data.combo_skill[from] = false
      data.extra_data.combo_skill[to] = true
      room:sendLog{ type = "#RmtPianshiLog", from = player.id, arg = from, arg2 = to }
    end
    if table.contains(cost_data.choices, "coincide") then
      room:setPlayerMark(player, "rmt__pianshi_coincide-turn", 1)
      player:drawCards(3, skel.name)
    end
  end,
})


return skel
